package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.FullyLinearLayoutManager;
import com.pansoft.openplanet.bean.TradeBean;
import com.pansoft.openplanet.widget.TCValueBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 交易 页面
 *@deprecated
 * @author YQS
 */
@Deprecated
public class TransactionActivity extends TCBaseActivity implements View.OnClickListener {
    private EditText etWeiTuoPrice;
    private EditText etTransactionCount;
    private EditText etTransactionPrice;
    private LinearLayout llTioami;
    private LinearLayout llDimi;
    private RecyclerView sellRecyclew;
    private RecyclerView buyRecyclew;
    private List<TradeBean> sellListData;
    private List<TradeBean> buyListData;
    private BuyAdapter buyAdapter;
    private SellAdapter sellAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_fragment_transaction);
        initView();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }

    private void initData() {
        buyListData = new ArrayList<>();
        sellListData = new ArrayList<>();


        sellListData.addAll(buyListData);

        buyAdapter = new BuyAdapter(this, R.layout.talk_chain_item_trade_trend, buyListData);
        sellAdapter = new SellAdapter(this, R.layout.talk_chain_item_trade_trend, sellListData);
        buyRecyclew.setAdapter(buyAdapter);
        sellRecyclew.setAdapter(sellAdapter);
        loadData();
    }

    private void loadData() {
        buyListData.clear();
        sellListData.clear();
        for (int i = 0; i < 5; i++) {
            TradeBean bean = new TradeBean.Builder().avatar("").count(String.valueOf(i + 0.21)).percent(5 + i * 8)
                    .title(ResStringUtil.getString(R.string.open_planet_asset_transaction_sale) + (i + 1)).userName(ResStringUtil.getString(R.string.open_planet_asset_transaction_user) + i).price("2503.97").build();
            sellListData.add(bean);

        }
        for (int i = 0; i < 5; i++) {
            TradeBean bean = new TradeBean.Builder().avatar("").count(String.valueOf(i + 0.21)).percent(80 - i * 5)
                    .title(ResStringUtil.getString(R.string.open_planet_asset_transaction_buy)+ (i + 1)).userName(ResStringUtil.getString(R.string.open_planet_asset_transaction_user) + i).price("2600.23").build();
            buyListData.add(bean);

        }


        buyAdapter.notifyDataSetChanged();
        sellAdapter.notifyDataSetChanged();
    }

    private void initView() {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction));
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);

        etWeiTuoPrice = (EditText) findViewById(R.id.et_weiTuoPrice);
        etTransactionCount = (EditText) findViewById(R.id.et_transactionCount);
        etTransactionPrice = (EditText) findViewById(R.id.et_transactionPrice);
        llTioami = (LinearLayout) findViewById(R.id.ll_tioami);
        llDimi = (LinearLayout) findViewById(R.id.ll_dimi);
        sellRecyclew = (RecyclerView) findViewById(R.id.sellRecyclew);
        buyRecyclew = (RecyclerView) findViewById(R.id.buyRecyclew);
//        sellRecyclew.setLayoutManager(new FullyLinearLayoutManager(this));
//        buyRecyclew.setLayoutManager(new FullyLinearLayoutManager(this));

        sellRecyclew.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, true));
        buyRecyclew.setLayoutManager(new LinearLayoutManager(this));
        llTioami.setOnClickListener(this);
        llDimi.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.ll_tioami) {
            Intent intent = new Intent(TransactionActivity.this, TransferActivity.class);
            intent.putExtra("myaddress", "");
            intent.putExtra("balance", "0");
            intent.putExtra("filePath", "");
            startActivity(intent);
        }
    }

    private class SellAdapter extends CommonAdapter<TradeBean> {
        public SellAdapter(Context context, int layoutId, List<TradeBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, TradeBean bean, int position) {

            TCValueBar tc_value_bar = holder.getView(R.id.tc_value_bar);
            tc_value_bar.setColor(R.color.tc_trade_green);
            TextView tvTradeNo = holder.getView(R.id.tv_trade_no);
            TextView tvTradeUserName = holder.getView(R.id.tv_trade_user_name);
            TextView tvTradePrice = holder.getView(R.id.tv_trade_price);
            TextView tvTradeAmount = holder.getView(R.id.tv_trade_amount);
            ImageView ivAvatar = holder.getView(R.id.iv_avatar);
            tvTradeUserName.setText(bean.getUserName());
            tvTradeNo.setText(bean.getTitle());
            tvTradePrice.setText(bean.getPrice());
            tc_value_bar.setValue(bean.getPercent());
            tvTradeAmount.setText(bean.getCount());
            tvTradeNo.setTextColor(getResources().getColor(R.color.tc_trade_green_text));

            // showAvatar(ivAvatar,bean.getAvatar());

        }
    }

    private class BuyAdapter extends CommonAdapter<TradeBean> {
        public BuyAdapter(Context context, int layoutId, List<TradeBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, TradeBean bean, int position) {
            TCValueBar tc_value_bar = holder.getView(R.id.tc_value_bar);
            tc_value_bar.setColor(R.color.tc_trade_red1);
            TextView tvTradeNo = holder.getView(R.id.tv_trade_no);
            TextView tvTradeUserName = holder.getView(R.id.tv_trade_user_name);
            TextView tvTradePrice = holder.getView(R.id.tv_trade_price);
            TextView tvTradeAmount = holder.getView(R.id.tv_trade_amount);
            ImageView ivAvatar = holder.getView(R.id.iv_avatar);

            tvTradeUserName.setText(bean.getUserName());
            tvTradeNo.setText(bean.getTitle());
            tvTradePrice.setText(bean.getPrice());
            tc_value_bar.setValue(bean.getPercent());
            tvTradeAmount.setText(bean.getCount());
            tvTradeNo.setTextColor(getResources().getColor(R.color.tc_trade_red_text));
        }
    }
}
